package com.tonghe.servlet.user;

import com.tonghe.pojo.User;
import com.tonghe.service.user.UserService;
import com.tonghe.service.user.UserServiceImpl;
import com.tonghe.util.Constants;
import org.w3c.dom.ls.LSOutput;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Date:2021/12/2- 12- 02- 21:37
 **/
public class LoginServlet extends HttpServlet {


    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //System.out.println("LoginServlet Start___...");
        String userCode = req.getParameter("userCode");
        String userPassword = req.getParameter("userPassword");
        User user = null;
        UserService userService = new UserServiceImpl();
        user = userService.login(userCode,userPassword);
        if(user != null && userPassword.equals(user.getUserPassword())){
//            System.out.println(user.getUserPassword());
            req.getSession().setAttribute(Constants.USER_SESSION,user);
            resp.sendRedirect("jsp/frame.jsp");
        }else{
            req.setAttribute("error","youwu");
            req.getRequestDispatcher("login.jsp").forward(req,resp);
        }

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
